define (require, exports, module) ->
  # 注册组件css
  require 'mods/reg/reg_css.css'
  # 服务接口
  reg_api =  require 'mods/reg/reg_api'
  # 图形验证码
  captcha_api = require 'mods/api/captcha_api'

  # 定义组件
  module.exports = Vue.extend
    # 变量声明
    data: () ->
      # 是否加载完成
      loadedSucc: false
      # 加载失败提示语
      loadedFail: ''
      # 错误类型  0无错，1用户名，2密码，3验证码，4手机号码，5电子邮箱，6证件类型，7证件号码，8证件开始日期，9证件结束日期
      errorType: 0
      # 注册信息
      user: {}
      # 图形验证码
      captchaSrc: ''
      # 图形验证码输入值
      captchaVal: ''
      # 工具条提示
      topTipsOption: {}
      # 用户选择的证件类型和证件类型名称
      id_obj: '-1'
      # 是否注册成功
      regStatu: false
      # 证件类型
      idCardType: [
        {
          'id_kind': '0',
          'id_kind_name': '身份证'
        }, {
          'id_kind': '1',
          'id_kind_name': '外国护照'
        }, {
          'id_kind': 'S',
          'id_kind_name': '港澳台居民身份证'
        }, {
          'id_kind': 'G',
          'id_kind_name': '港澳居民来往内地通行证'
        }, {
          'id_kind': 'H',
          'id_kind_name': '台湾居民来往大陆通行证'
        }
      ]
      # 身份证地区
      aCity:
        11: '北京'
        12: '天津'
        13: '河北'
        14: '山西'
        15: '内蒙古'
        21: '辽宁'
        22: '吉林'
        23: '黑龙江'
        31: '上海'
        32: '江苏'
        33: '浙江'
        34: '安徽'
        35: '福建'
        36: '江西'
        37: '山东'
        41: '河南'
        42: '湖北'
        43: '湖南'
        44: '广东'
        45: '广西'
        46: '海南'
        50: '重庆'
        51: '四川'
        52: '贵州'
        53: '云南'
        54: '西藏'
        61: '陕西'
        62: '甘肃'
        63: '青海'
        64: '宁夏'
        65: '新疆'
        71: '台湾'
        81: '香港'
        82: '澳门'
        91: '国外'

    # 组件模板
    template: require 'mods/reg/reg_tpl.js'

    # 组件方法
    methods:

      # 弹出错误提示
      alertTipError: (content) ->
        self = @
        self.alertTip 'error', content
        return false

      # 弹出成功提示
      alertTipSuccess: (content) ->
        self = @
        self.alertTip 'success', content
        return false

      # 弹出提示
      alertTip: (tip_type, content) ->
        self = @
        # 工具条提示
        topTipsObj =
          state: true
          auto_close: true
          tip_type: tip_type
          content: content
        self.topTipsOption = Vue.util.extend {}, topTipsObj

      # 判断注册错误类型
      checkErrorType: (errInfo) ->
        self = @
        if -1 isnt errInfo?.indexOf '用户名'
          self.errorType = 1
        else if -1 isnt errInfo?.indexOf '密码'
          self.errorType = 2
        else if -1 isnt errInfo?.indexOf '验证码'
          self.errorType = 3
        else if -1 isnt errInfo.indexOf '手机号码'
          self.errorType = 4
        else if -1 isnt errInfo?.indexOf '邮箱'
          self.errorType = 5
        else if -1 isnt errInfo?.indexOf '证件号码'
          self.errorType = 7

      # 执行注册
      doRegister: () ->
        self = @

        self.user.id_kind = self.id_obj?.id_kind
        self.user.id_kind_name = self.id_obj?.id_kind_name

        # 前端校验证件类型和证件号是否为空
        if '-1' is self.id_obj
          self.alertTipError '请选择证件类型'
          self.errorType = 6
          return
        else if !self.user.id_no
          self.alertTipError '证件号码必填'
          self.errorType = 7
          return
        else if !self.user.id_begindate
          self.alertTipError '证件开始日期必填'
          self.errorType = 8
          return
        else if !self.user.id_enddate
          self.alertTipError '证件结束日期必填'
          self.errorType = 9
          return

        flag = self.isCardID self.user.id_no

        if '0' is self.user.id_kind and true isnt flag
          self.alertTipError flag
          self.errorType = 7
          return

        # 注册参数
        params =
          user: self.user
          captcha: self.captchaVal

        # 提交注册
        reg_api.doReg params
          .then (res) ->
            if 200 isnt res?.status
              self.refreshCaptcha()
              self.alertTipError res?.rejects?[0].error or res?.data?.error or '注册失败'
              self.checkErrorType res?.rejects?[0].error or res?.data?.error or '注册失败'
            else
              self.regStatu = true
          .catch (err) ->
            self.refreshCaptcha()
            self.alertTipError err?.data?.rejects?[0].error or err?.data?.error or '未知的异常，请重新登录'
            self.checkErrorType err?.data?.rejects?[0].error or err?.data?.error or '未知的异常，请重新登录'
            console.log 'err:',err

      # 刷新验证码
      refreshCaptcha: () ->
        self = @
        self.captchaSrc = captcha_api.getCaptcha()

      # 验证身份证的合法性
      isCardID : (sId) ->
        self = @
        iSum = 0
        info = ''
        if !/^\d{17}(\d|x)$/i.test(sId)
          return '你输入的身份证长度或格式错误'
        sId = sId.replace(/x$/i, 'a')
        if self.aCity[parseInt(sId.substr(0, 2))] == null
          return '你的身份证地区非法'
        sBirthday = sId.substr(6, 4) + '-' + Number(sId.substr(10, 2)) + '-' + Number(sId.substr(12, 2))
        d = new Date(sBirthday.replace(/-/g, '/'))
        if sBirthday != d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
          return '身份证上的出生日期非法'
        i = 17
        while i >= 0
          iSum += 2 ** i % 11 * parseInt(sId.charAt(17 - i), 11)
          i--
        if iSum % 11 != 1
          return '你输入的身份证号非法'
        true

    # 实例创建时触发
    created: () ->
      self = @
      self.refreshCaptcha()

    components:
      'c-top-tips': require 'mods/top_tips/top_tips_mod'

    # 计算属性
    computed:
      # 加载中的样式
      loaddingClass: () ->
        self = @
        if self.loadedSucc
          return 'c-hidden'
        if self.loadedFail
          return 'c-hidden'
        return ''

      # 加载成功的样式
      loadedSuccClass: () ->
        self = @
        return if self.loadedSucc then '' else 'c-vhidden'

      # 加载失败的样式
      loadedFailClass: () ->
        self = @
        return if self.loadedFail then '' else 'c-hidden'

    # 组件初始化执行
    mounted: () ->
      self = @
      setTimeout () ->
        Vue.nextTick () ->
          # 加载成功标识
          self.loadedSucc = true
      , 5